<template>
  <d2-container>
    <div class="fundsManagement">
      <el-tabs type="border-card" @tab-click="onTabClick">
        <el-tab-pane label="任务扣款">
          <span>日期：</span>
          <el-date-picker size="small" v-model="timearr" type="daterange" align="right" unlink-panels range-separator="至"
                          start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions"
                          format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"></el-date-picker>
          <el-button style="margin-left:16px;" size="small" type="primary" icon="el-icon-search" @click="onkkSearch">搜索</el-button>
          <div class="tablebox">
            <el-table v-loading="kkloading" :data="tableDatakk" border style="width: 100%" :height="tableHeight" header-align="center">
              <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
              <el-table-column prop="id" label="任务ID" width="100" align="center"></el-table-column>
              <el-table-column prop="cooperation_message" label="任务详情" align="center"></el-table-column>
              <el-table-column prop="cooperation_money" label="扣款前余额" width="120" align="center"></el-table-column>
              <el-table-column prop="cooperationlog_time" label="扣款时间" width="200" align="center"></el-table-column>
              <el-table-column prop="cooperation_note" label="操作" width="200" align="center"></el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
        <el-tab-pane label="任务返款">
          <span>日期：</span>
          <el-date-picker size="small" v-model="timelist" type="daterange" align="right" unlink-panels range-separator="至"
                          start-placeholder="开始日期" end-placeholder="结束日期"
                          format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"></el-date-picker>
          <span style="margin-left:16px;">支付ID：</span>
          <el-input size="small" v-model="payval" placeholder="请输入支付ID" style="width:200px;"></el-input>
          <el-button style="margin-left:16px;" size="small" type="primary" icon="el-icon-search" @click="onSearch">搜索</el-button>
          <div class="tablebox">
            <el-table v-loading="tkloading" :data="tableDatatk" border style="width: 100%" :height="tableHeight" header-align="center">
              <el-table-column type="index" label="序号" width="60" align="center"></el-table-column>
              <el-table-column prop="pay_task_id" label="支付ID" width="100" align="center"></el-table-column>
              <el-table-column prop="goodsname" label="商品名称" align="center"></el-table-column>
              <el-table-column prop="shopname" label="店铺名称" align="center"></el-table-column>
              <el-table-column prop="account_num" label="返款单数" width="100" align="center"></el-table-column>
              <el-table-column prop="account_money" label="返款金额" width="120" align="center"></el-table-column>
              <el-table-column prop="create_time" label="返款时间" width="200" align="center"></el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
      <el-pagination @current-change="handleCurrentChange" :current-page.sync="page" style="text-align:right;margin-top:10px;"
                     :page-size="20" layout="total, prev, pager, next" :total="total"></el-pagination>
    </div>
  </d2-container>
</template>

<script>
  import { accontlist,paylist } from '../../api/api'
  import Vue from "vue";
  export default {
    data() {
      return {
        tableHeight:document.body.clientHeight-280 + 'px',
        timelist:[],
        timearr:[],
        tkloading:false,
        kkloading:false,
        tableDatatk:[],
        tableDatakk:[],
        page:1,
        total:0,
        payval:''
      }
    },
    mounted(){
      this.getkklist()
      this.getUser()
    },
    methods: {
      //获取用户信息
      getUser(){
        userinfo().then(res=>{
          if(res.Status == 200){
            this.$emit('getMoney',res.Data.money)
          }else{
            this.$message.error(res.Message)
          }
        })
      },
      //获取任务扣款列表
      getkklist(){
        this.kkloading = true
        let params = {
          page:this.page,
          paramenters:this.timearr,
        }
        paylist(params).then(res=>{
          this.kkloading = false
          if(res.Status == 200){
            this.total = res.Count
            let temp = res.Data
            let ipos
            temp.forEach(ele => {
              ipos = ele.cooperationlog_time.indexOf("+")
              ele.cooperationlog_time = ele.cooperationlog_time.replace(/T/g," ").substring(0,ipos)
            })
            this.tableDatakk = temp

          }else{
            this.$message.error(res.Message)
          }
        })
      },
      //获取任务返款
      getData(){
        this.tkloading = true
        let params = {
          page:this.page,
          paramenters:this.timelist,
          task_id:this.payval ? this.payval : 0
        }
        accontlist(params).then(res=>{
          this.tkloading = false
          if(res.Status == 200){
            this.total = res.Count
            let temp = res.Data
            let ipos
            temp.forEach(ele => {
              ipos = ele.create_time.indexOf("+")
              ele.create_time = ele.create_time.replace(/T/g," ").substring(0,ipos)
            })
            this.tableDatatk = temp

          }else{
            this.$message.error(res.Message)
          }
        })
      },
      //扣款搜索
      onkkSearch(){
        if(this.timearr.length>0){
          this.timearr[0] = this.timearr[0] + ' 00:00:00'
          this.timearr[1] = this.timearr[1] + ' 23:59:59'
        }
        this.getkklist()
      },
      //返款搜索
      onSearch(){
        if(this.timelist.length>0){
          this.timelist[0] = this.timelist[0] + ' 00:00:00'
          this.timelist[1] = this.timelist[1] + ' 23:59:59'
        }
        this.getData()
      },
      //切换tabs
      onTabClick(val){
        this.page = 1
        if(val.label == '任务扣款'){
          this.getkklist()
        }else if(val.label == '任务返款'){
          this.getData()
        }
      },
      //分页
      handleCurrentChange(val){
        this.page = val
        this.getkklist()
      }
    }
  };
</script>
<style lang="scss">
  .fundsManagement {
    .tablebox{
      margin:20px 0;
    }
  }
</style>
